#include "at32f421_ertc.h"
#include "at32f421_crm.h"
#include <string.h>
#include "flapp_system.h"

__IO uint16_t ertc_clk_div_a = 0;
__IO uint16_t ertc_clk_div_b = 0;

/* select the ertc clock source */
#define ERTC_CLOCK_SOURCE_LEXT           /* select lext as the ertc clock */
//#define ERTC_CLOCK_SOURCE_LICK         /* select lick as the ertc clock */

static void fldrv_ertc_config(void)
{
#if defined (ERTC_CLOCK_SOURCE_LICK)
  /* enable the lick osc */
  crm_clock_source_enable(CRM_CLOCK_SOURCE_LICK, TRUE);

  /* wait till lick is ready */
  while(crm_flag_get(CRM_LICK_STABLE_FLAG) == RESET)
  {
  }

  /* select the ertc clock source */
  crm_ertc_clock_select(CRM_ERTC_CLOCK_LICK);

  /* ertc second(1hz) = ertc_clk(lick) / (ertc_clk_div_a + 1) * (ertc_clk_div_b + 1) */
  ertc_clk_div_b = 255;
  ertc_clk_div_a = 127;
#elif defined (ERTC_CLOCK_SOURCE_LEXT)
  /* enable the lext osc */
  crm_clock_source_enable(CRM_CLOCK_SOURCE_LEXT, TRUE);

  /* wait till lext is ready */
  while(crm_flag_get(CRM_LEXT_STABLE_FLAG) == RESET)
  {
  }

  /* select the ertc clock source */
  crm_ertc_clock_select(CRM_ERTC_CLOCK_LEXT);

  /* ertc second(1hz) = ertc_clk / (ertc_clk_div_a + 1) * (ertc_clk_div_b + 1) */
  ertc_clk_div_b = 255;
  ertc_clk_div_a = 127;
#endif

  /* enable the ertc clock */
  crm_ertc_clock_enable(TRUE);

  /* deinitializes the ertc registers */
  ertc_reset();

  /* wait for ertc registers update */
  ertc_wait_update();

  /* configure the ertc divider */
  ertc_divider_set(ertc_clk_div_a, ertc_clk_div_b);

  /* configure the ertc hour mode */
  ertc_hour_mode_set(ERTC_HOUR_MODE_24);

  /* set date: 2021-05-01 */
  ertc_date_set(21, 5, 1, 5);

  /* set time: 12:00:00 */
  ertc_time_set(12, 0, 0, ERTC_AM);
}

static void fldrv_alarm_config(void)
{
  exint_init_type exint_init_struct;

  /* config the exint line of the ertc alarm */
  exint_init_struct.line_select   = EXINT_LINE_17;
  exint_init_struct.line_enable   = TRUE;
  exint_init_struct.line_mode     = EXINT_LINE_INTERRUPUT;
  exint_init_struct.line_polarity = EXINT_TRIGGER_RISING_EDGE;
  exint_init(&exint_init_struct);

  /* set the alarm 05h:20min:10s */
  ertc_alarm_mask_set(ERTC_ALA, ERTC_ALARM_MASK_DATE_WEEK | ERTC_ALARM_MASK_HOUR | ERTC_ALARM_MASK_MIN);
  ertc_alarm_week_date_select(ERTC_ALA, ERTC_SLECT_DATE);
//  ertc_alarm_set(ERTC_ALA, 31, 6, 20, 5, ERTC_AM);

  /* enable the ertc interrupt */
  nvic_irq_enable(ERTC_IRQn, 4, 0);

  /* enable ertc alarm a interrupt */
  ertc_interrupt_enable(ERTC_ALA_INT, TRUE);

  /* enable the alarm */
  ertc_alarm_enable(ERTC_ALA, TRUE);
}

void fldrv_ertc_init(void)
{
    crm_periph_clock_enable(CRM_PWC_PERIPH_CLOCK, TRUE);
    /* allow access to ertc */
    pwc_battery_powered_domain_access(TRUE);
    /* reset ertc domain */
    crm_battery_powered_domain_reset(TRUE);
    crm_battery_powered_domain_reset(FALSE);
    fldrv_ertc_config();
    fldrv_alarm_config();
}


/**
  * @brief  display the current time.
  * @param  none
  * @retval none
  */
void flapp_ertc_time_show(void)
{
  static u8 temp = 0;
  ertc_time_type time;

  /* get the current time */
  ertc_calendar_get(&time);

  if(temp == time.sec)  return;
  temp = time.sec;
  /* display date format : year-month-day */
  log_debug("Time:%d-%02d-%02d %02d:%02d:%02d",time.year, time.month, time.day,time.hour, time.min, time.sec);
}

/**
  * @brief  display the current alarm.
  * @param  none
  * @retval none
  */
void flapp_ertc_alarm_show(void)
{
  ertc_alarm_value_type alarm;

  /* get the current alarm */
  ertc_alarm_get(ERTC_ALA, &alarm);

  /* display alarm format : hour:min:sec */
  log_debug("Alarm: %02d:%02d:%02d\r\n",alarm.hour, alarm.min, alarm.sec);
}

